<template>
  <div class="order">
    <!-- 查询 -->
    <el-form
      :inline="true"
      size="small"
      :model="searchData"
      class="demo-form-inline"
    >
      <!-- 订单号 -->
      <el-form-item label="订单号">
        <el-input v-model="searchData.orderNo" placeholder="订单号"></el-input>
      </el-form-item>

      <!-- 收货人 -->
      <el-form-item label="收货人">
        <el-input
          v-model="searchData.consignee"
          placeholder="收货人"
        ></el-input>
      </el-form-item>

      <!-- 手机号 -->
      <el-form-item label="手机号">
        <el-input v-model="searchData.phone" placeholder="手机号"></el-input>
      </el-form-item>

      <!-- 订单状态 -->
      <el-form-item label="订单状态">
        <el-select v-model="searchData.orderState" placeholder="订单状态">
          <el-option label="未受理" value="未受理"></el-option>
          <el-option label="已受理" value="已受理"></el-option>
          <el-option label="派送中" value="派送中"></el-option>
          <el-option label="已完成" value="已完成"></el-option>
          <el-option label="已送达" value="已送达"></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <!-- 选择时间 -->
    <el-form :inline="true" size="small" :model="searchData" class="time">
      <el-form-item label="选择时间">
        <el-date-picker
          v-model="searchData.date"
          type="datetimerange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          value-format="yyyy-MM-dd HH:mm:ss"
        >
        </el-date-picker>
      </el-form-item>
      <el-button type="primary" @click="onSearch" size="small">查询</el-button>
    </el-form>

    <!-- 列表 -->
    <el-table
      :data="tableData"
      border
      :style="{ width: w + 'px' }"
      :highlight-current-row="true"
    >
      <!-- 订单号 -->
      <el-table-column fixed prop="orderNo" label="订单号" width="150">
      </el-table-column>
      <!-- 下单时间 -->
      <el-table-column label="下单时间" width="150">
        <template slot-scope="scope">
          {{ scope.row.orderTime }}
        </template>
      </el-table-column>
      <!-- 手机号 -->
      <el-table-column prop="phone" label="手机号" width="150">
      </el-table-column>
      <!-- 收货人 -->
      <el-table-column prop="consignee" label="收货人" width="150">
      </el-table-column>
      <!-- 配送地址 -->
      <el-table-column prop="deliverAddress" label="配送地址">
      </el-table-column>
      <!-- 送达时间 -->
      <el-table-column label="送达时间" width="150">
        <template slot-scope="scope">
          {{ scope.row.deliveryTime }}
        </template>
      </el-table-column>
      <!-- 用户备注 -->
      <el-table-column prop="remarks" label="用户备注" width="150">
      </el-table-column>
      <!-- 订单金额 -->
      <el-table-column prop="orderAmount" label="订单金额" width="150">
      </el-table-column>
      <!-- 订单状态 -->
      <el-table-column prop="orderState" label="订单状态" width="150">
      </el-table-column>
      <!-- 操作 -->
      <el-table-column fixed="right" label="操作" width="100">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small"
            >查看</el-button
          >
          <el-button type="text" size="small">编辑</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <div class="mt20 mb50">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        layout="total, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { reqOrderList } from "@/api/order";
import { parseTime } from "@/utils/date";
export default {
  data() {
    return {
      searchData: {
        orderNo: "",
        phone: "",
        consignee: "",
        orderState: "",
        date: [],
      },
      tableData: [],

      //表单适应显示器的宽度
      w: document.body.clientWidth - 220,

      //分页
      currentPage: 1, //当前页码值,一定要以1开始
      pageSize: 10, //每一页显示多少条
      total: 0, //总共条数
    };
  },
  methods: {
    //获取订单数据
    async getData() {
      console.log(this.searchData.date);
      //2.发送ajax
      let res = await reqOrderList({
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        ...this.searchData,
        date:
          this.searchData.date == null
            ? JSON.stringify([])
            : JSON.stringify(this.searchData.date),
      });

      //3.解构
      let { data, total } = res.data;

      //判定---如果把当前页数据清空后,需要回拉一页,显示前一页的数据,如果已经是第一页,不操作
      if (!data.length && this.currentPage != 1) {
        this.currentPage -= 1; //回拉一页
        this.getData(); //重新渲染数据
      }

      //4.处理数据格式
      console.log(data);
      data.map((v) => {
        v.deliveryTime = parseTime(v.deliveryTime);
        v.orderTime = parseTime(v.orderTime);
      });
      //5.把处理好的数据放到指定位置
      this.tableData = data;
      this.total = total;
    },

    //查询
    onSearch() {
      this.currentPage = 1;
      this.getData();
    },
    handleClick(row) {
      console.log(row);
    },
    //动态计算宽度
    resizeWidth() {
      // console.log(document.body.clientWidth);
      //把当前屏幕宽度-左菜单和margin的距离= 右边的宽度
      this.w = document.body.clientWidth - 220;
    },

    // 分页
    handleSizeChange(val) {
      this.pageSize = val;
      this.getData();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getData();
    },
  },

  //生命周期监听width的变化
  created() {
    //进入页面获取订单数据
    this.getData();
    //添加window调整窗口大小的事件
    window.addEventListener("resize", this.resizeWidth);
  },

  //生命周期销毁监听事件
  beforeDestroy() {
    window.removeEventListener("resize", this.resizeWidth);
  },
};
</script>

<style lang="less" scoped>
.order {
  background-color: #fff;
  padding-bottom: 50px;
}
.demo-form-inline {
  padding: 20px 20px 0px 20px;
}
.mt20 {
  margin-top: 20px;
}
.time {
  padding: 0px 0px 0px 20px;
}

</style>